#include <iostream>
#include <vector>
#include <map>

struct APP
{
    std::string app_name;
    int prio;
    std::string st_tm;
    std::string ed_tm;
};

int main()
{
    int N;
    std::cin >> N;

    std::vector<APP> app_vec;
    for (int i = 0; i < N; i++)
    {
        APP app_tmp;
        std::cin >> app_tmp.app_name >> app_tmp.prio >> app_tmp.st_tm >> app_tmp.ed_tm;
        app_vec.push_back(app_tmp);
    }

    std::string tm;
    std::cin >> tm;

    std::vector<APP> app_reg;
    for (int i = 0; i < N; i++)
    {
        bool can_reg = true;
        for (int j = 0; j < app_reg.size(); j++)
        {
            if (app_vec[i].st_tm <= app_reg[j].ed_tm && app_vec[i].ed_tm >= app_reg[j].st_tm)
            {
                if (app_vec[i].prio <= app_reg[j].prio)
                {
                    can_reg = false;
                    break;
                }
            }
        }

        if (can_reg)
        {
            std::vector<APP> app_tmp;
            for (int j = 0; j < app_reg.size(); j++)
            {
                if (!(app_vec[i].st_tm <= app_reg[j].ed_tm && app_vec[i].ed_tm >= app_reg[j].st_tm))
                {
                    app_tmp.push_back(app_reg[j]);
                }
            }
            app_tmp.push_back(app_vec[i]);
            app_reg.clear();
            app_reg = app_tmp;
            app_tmp.clear();
        }
    }

    bool find = false;
    for (int i = 0; i < app_reg.size(); i++)
    {
        if (tm <= app_reg[i].ed_tm && tm >= app_reg[i].st_tm)
        {
            find = true;
            std::cout << app_reg[i].app_name << std::endl;
            break;
        }
    }

    if (!find)
        std::cout << "NA" << std::endl;

    return 0;
}